package loan.calc.calculator.common;



import loan.calc.constant.dict.RepayMethodDictEnum;
import loan.calc.constant.dict.rate.RateTypeEnum;
import loan.calc.constant.dict.rate.RateUnitEnum;
import loan.calc.dto.LoanRateSegmentDTO;
import loan.calc.util.BigDecimalUtils;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;

public class InterestRateCalculator {

    public static BigDecimal getYearRate(BigDecimal val, RateUnitEnum unit, Integer yearBaseDays) {
        switch (unit) {
            case YEAR_RATE:
                return val;
            case MONTH_RATE:
                return val.multiply(BigDecimal.valueOf(12), new MathContext(RateUnitEnum.YEAR_RATE.getScale(), RoundingMode.HALF_UP));
            case DAY_RATE:
                return val.multiply(BigDecimal.valueOf(yearBaseDays), new MathContext(RateUnitEnum.YEAR_RATE.getScale(), RoundingMode.HALF_UP));
            default:
                throw new IllegalArgumentException("Unexpected unit: " + unit);
        }
    }

    public static BigDecimal getMonthRate(BigDecimal val, RateUnitEnum unit, Integer yearBaseDays) {
        // 没有月基准天数，先算年利率 ，在算月利率
        switch (unit) {
            case YEAR_RATE:
                return val.divide(BigDecimal.valueOf(12), RateUnitEnum.MONTH_RATE.getScale(), RoundingMode.HALF_UP);
            case MONTH_RATE:
                return val;
            case DAY_RATE:
                return getYearRate(val, unit, yearBaseDays).divide(BigDecimal.valueOf(12), RateUnitEnum.MONTH_RATE.getScale(), RoundingMode.HALF_UP);
            default:
                throw new IllegalArgumentException("Unexpected unit: " + unit);
        }
    }

    public static BigDecimal getDayRate(BigDecimal val, RateUnitEnum unit, Integer yearBaseDays) {
        switch (unit) {
            case YEAR_RATE:
                return val.divide(BigDecimal.valueOf(yearBaseDays), RateUnitEnum.DAY_RATE.getScale(), RoundingMode.HALF_UP);
            case MONTH_RATE:
                return getYearRate(val, unit, yearBaseDays).divide(BigDecimal.valueOf(yearBaseDays), RateUnitEnum.DAY_RATE.getScale(), RoundingMode.HALF_UP);
            case DAY_RATE:
                return val;
            default:
                throw new IllegalArgumentException("Unexpected unit: " + unit);
        }
    }

    /**
     * 计算单位期次利率
     * 计息基础金额为1 通过利息计算器计算所得即为利率
     * @param rateSegmentList 利率信息列表
     * @param startDate 单位周期开始时间
     * @param endDate   单位周期结束时间
     * @param repayMethod   还款方式
     * @return 单期次利率 保留八位小数
     */
    public static BigDecimal obtainPeriodInterestRate(List<LoanRateSegmentDTO> rateSegmentList, LocalDate startDate, LocalDate endDate, RepayMethodDictEnum repayMethod) {
        BigDecimal interestRate = InterestCalculator.calculateInterest(BigDecimal.ONE, startDate, endDate, rateSegmentList, repayMethod.getPeriodSplitMode(), RateTypeEnum.NORMAL_RATE);
        interestRate = interestRate.setScale(BigDecimalUtils.RATE_SCALE, BigDecimalUtils.DEFAULT_CURRENCY_ROUNDING_MODE);
        return interestRate;
    }


}
